﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Modeling_Sub_Pages_NewsPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        try
        {
            if (!IsPostBack)
            {
                int id = Convert.ToInt32(Request.QueryString["id"].ToString());
                if (udm.createConnection() != null && udm.showCoursesByID(udm.createConnection(), id) != null)
                {
                    dlEvents.DataSource = udm.showCoursesByID(udm.createConnection(), id);
                    dlEvents.DataBind();
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                } 
            }
        }
        catch (Exception)
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void dlEvents_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        Label lblMonday = (Label)e.Item.FindControl("lblMonday");
        Label lblTuesday = (Label)e.Item.FindControl("lblTuesday");
        Label lblWednesday = (Label)e.Item.FindControl("lblWednesday");
        Label lblThursday = (Label)e.Item.FindControl("lblThursday");
        Label lblFriday = (Label)e.Item.FindControl("lblFriday");
        Label lblSaturday = (Label)e.Item.FindControl("lblSaturday");
        Label lblSunday = (Label)e.Item.FindControl("lblSunday");
        Label lblEnd = (Label)e.Item.FindControl("lblEnd");

        Button btnSignUp = (Button)e.Item.FindControl("btnSignUp");

        btnSignUp.OnClientClick = string.Format("return confirm('Are you certain you want to sign up this course?')");
        
        if (lblMonday.Text.Equals("True"))
        {
            //lblMonday.BackColor = System.Drawing.Color.Gray;
            //lblMonday.ForeColor = System.Drawing.Color.White;
            //lblMonday.Text = "M";
            lblMonday.Text = "Mon";
        }
        else
        {
            //lblMonday.Text = "M";
            lblMonday.Visible = false;
        }

        if (lblTuesday.Text.Equals("True"))
        {
            //lblTuesday.BackColor = System.Drawing.Color.Gray;
            //lblTuesday.ForeColor = System.Drawing.Color.White;
            //lblTuesday.Text = "T";
            lblTuesday.Text = "Tue";
        }
        else
        {
            //lblTuesday.Text = "T";
            lblTuesday.Visible = false;
        }

        if (lblWednesday.Text.Equals("True"))
        {
            //lblWednesday.BackColor = System.Drawing.Color.Gray;
            //lblWednesday.ForeColor = System.Drawing.Color.White;
            //lblWednesday.Text = "W";
            lblWednesday.Text = "Wed";
        }
        else
        {
            //lblWednesday.Text = "W";
            lblWednesday.Visible = false;
        }

        if (lblThursday.Text.Equals("True"))
        {
            //lblThursday.BackColor = System.Drawing.Color.Gray;
            //lblThursday.ForeColor = System.Drawing.Color.White;
            //lblThursday.Text = "T";
            lblThursday.Text = "Thu";
        }
        else
        {
            //lblThursday.Text = "T";
            lblThursday.Visible = false;
        }

        if (lblFriday.Text.Equals("True"))
        {
            //lblFriday.BackColor = System.Drawing.Color.Gray;
            //lblFriday.ForeColor = System.Drawing.Color.White;
            //lblFriday.Text = "F";
            lblFriday.Text = "Fri";
        }
        else
        {
            //lblFriday.Text = "F";
            lblFriday.Visible = false;
        }

        if (lblSaturday.Text.Equals("True"))
        {
            //lblSaturday.BackColor = System.Drawing.Color.Gray;
            //lblSaturday.ForeColor = System.Drawing.Color.White;
            //lblSaturday.Text = "S";
            lblSaturday.Text = "Sat";
        }
        else
        {
            //lblSaturday.Text = "S";
            lblSaturday.Visible = false;
        }

        if (lblSunday.Text.Equals("True"))
        {
            //lblSunday.BackColor = System.Drawing.Color.Gray;
            //lblSunday.ForeColor = System.Drawing.Color.White;
            //lblSunday.Text = "S";
            lblSunday.Text = "Sun";
        }
        else
        {
            //lblSunday.Text = "S";
            lblSunday.Visible = false;
        }
    }
    protected void dlEvents_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "SignUp")
        {
            int id = Convert.ToInt32(Request.QueryString["id"].ToString());
            UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

            if (udm.createConnection() != null && udm.showCoursesByID(udm.createConnection(), id) != null)
            {
                if (Convert.ToBoolean(udm.showCoursesByID(udm.createConnection(), id).Tables[0].Rows[0][19].ToString()))
                {
                    Response.Redirect("~/LoginMessage.aspx?id=2");
                }
                else
                {
                    if (Session["WelcomeUser"] != null)
                    {
                        if (Session["StudentID"] != null)
                        {
                            string studentID = Session["StudentID"].ToString();

                            if (udm.checkCoursesRegisByStudentID(udm.createConnection(), id, studentID).Tables[0].Rows.Count == 0)
                            {
                                if (udm.addRegistraionCourse(udm.createConnection(), id, studentID))
                                {
                                    Response.Redirect("~/Students/CourseSignUp.aspx");
                                }
                                else
                                {
                                    Response.Redirect("~/ErrorPage.aspx");
                                }
                            }
                            else
                            {
                                Response.Redirect("~/LoginMessage.aspx?id=3");
                            }
                        }
                        else
                        {
                            Response.Redirect("~/LoginMessage.aspx?id=1");
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Login.aspx?id=1");
                    }
                }
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
        }
    }
}